import json

from myapp import models


def getPageData():
    jobs = models.JobInfo.objects.all()
    data = [j.type for j in jobs]
    return list(set(data))




def getBarTagData(jobType):
    if jobType == '不限':
        jobs = models.JobInfo.objects.all()
    else:
        jobs = models.JobInfo.objects.filter(type=jobType).all()

    data = {}
    for j in jobs:
        workTag = json.loads(j.workTag)
        for w in workTag:
            if not w:
                continue
            if data.get(w, -1) == -1:
                data[w] = 1
            else:
                data[w] += 1

    result = sorted(data.items(), key=lambda x: x[1], reverse=True)[:20]
    print(result)
    rowData = []
    columnData = []
    for k, v in result:
        rowData.append(k)
        columnData.append(v)

    return rowData, columnData


def getPieStatusData(jobType):
    if jobType == '不限':
        jobs = models.JobInfo.objects.all()
    else:
        jobs = models.JobInfo.objects.filter(type=jobType).all()

    data = {}
    for j in jobs:
        if data.get(j.companyStatus, -1) == -1:
            data[j.companyStatus] = 1
        else:
            data[j.companyStatus] += 1

    result = []
    for k, v in data.items():
        result.append({
            'name': k,
            'value': v
        })
    return result